#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        if (!root) return {};
        queue<Node*> q;
        q.push(root);
        vector<vector<int>> res;
        while (!q.empty())
        {
            int sz = q.size();
            vector<int> v;
            v.reserve(sz);
            for (int i = 0; i < sz; i++)
            {
                Node* cur = q.front();
                v.push_back(cur->val);
                q.pop();
                for (auto it : cur->children)
                {
                    q.push(it);
                }
            }
            res.push_back(v);
        }
        return res;
    }
};